/****************************************************************************
 * @file    : lin_identification_and_configuration.h
 * @author  : Nonosns MCU Team
 * @version : V2.0
 * @Date    : 2023/4/7
 * @brief   : lin stack file
 * @note
 * Copyright (C) 2023 Novosense All rights reserved.
 ****************************************************************************/
#ifndef LIN_IDENTIFICATION_AND_CONFIGURATION_H
#define LIN_IDENTIFICATION_AND_CONFIGURATION_H
/*******************************************************************************
  type definitions
*******************************************************************************/
#include <stdint.h>

#ifdef L_SNPD_BSM
// definition of 'g_bsmNodeState'
#define L_NODE_BSM_OFF 0     // initial state: BSM inactive
#define L_NODE_INIT 1        // not addressed, not pre-selected
#define L_NODE_SELECTED 2    // pre-selected
#define L_NODE_ADDRESSED 3   // addressed
#define L_NODE_NAD_STORED 4  // NAD accepted and stored to RAM

/* TODO, should configurate threahold by NSUC16X0's PGA and ADC
     - Reduce PGA gain to 10 (to be able to measure up to 25mA)
     - Adjust current thresholds due to impact of current source and/or pull-up
     - Effective R_BSM is 1 Ohm (including the two bond wires)


     Calculation of resulting thresholds (in LSB):
        for ISH2:
          1.0mA through 1.2 Ohm shunt results in 1.2mV:
          voltage @ ADC = 1.2mV * 8 (LINBSM gain) * 10 (PGA gain) = 96mV
          ADC value [in LSB] = 96mV * 4096/2V = 196 LSB
        for ISH3:
          4.5mA through 1.2 Ohm shunt results in 5.4mV:
          voltage @ ADC = 5.4mV * 8 (LINBSM gain) * 10 (PGA gain) = 432mV
          ADC value [in LSB] = 432mV * 4096/2V = 882 LSB */

#define SNPD_I_DIFF 450  // corresponds to 2.3 mA via 1.2 Ohm

#endif

#ifdef L_CONFIG_IDENTIFIER
typedef struct {
    uint8_t SID;
    void (*ConfigFunc)(void);
} SidToConfigFunc;

#define NAD_INDEX_IN_DIAGNOSIS_FRAME 0x00
#define PCI_INDEX_IN_DIAGNOSIS_FRAME 0x01
#define SID_INDEX_IN_DIAGNOSIS_FRAME 0x02
#define DATA_0_IN_DIAGNOSIS_FRAME 0x03
#define DATA_1_IN_DIAGNOSIS_FRAME 0x04
#define DATA_2_IN_DIAGNOSIS_FRAME 0x05
#define DATA_3_IN_DIAGNOSIS_FRAME 0x06
#define DATA_4_IN_DIAGNOSIS_FRAME 0x07
#define SF_MAX_PCI 0x06
/* Ncr = 1000ms */
#define TP_LAYER_N_CR_TIMEOUT 10

void LIN_TpLayerNCrTimeoutScheduler(void);
void LIN_NodeConfigAndIndentifByDiagnosticFrame(void);
void BSM_ResetInFrameEnd(void);
l_u8 BSM_GetBSMNodeStatus(void);
#endif

l_u8 LIN_GetResponseFlagForDiagnosisFrame(void);
void LIN_ClearResponseFlagForDiagnosisFrame(void);

l_u8 LIN_GetSaveNADRequest(void);
l_u8 LIN_GetSavePIDRequest(void);
void LIN_ClearSaveNADRequest(void);
void LIN_ClearSavePIDRequest(void);
#endif
